/*
 * AddressProvider.java
 * business
 *
 * Created by ChenTao on 2017/3/11.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.recyclerview.provider;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.address.Area;
import com.yidiandao.business.ui.widget.address.AddressPager;

import me.drakeet.multitype.ItemViewProvider;

/**
 * 选择地址的Provider
 *
 * <p>Created by ChenTao(chentao7v@gmail.com) on 2017/3/11 0011 13:44
 */
public class ChooseAddressProvider
        extends ItemViewProvider<Area, ChooseAddressProvider.ViewHolder> {

    private AddressPager.OnItemClickListener listener;

    public void setOnItemClickListener(AddressPager.OnItemClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_dialog_choose_address, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull Area area) {
        holder.area = area;
        holder.listener = listener;
        holder.itemView.setSelected(area.isSelected());
        holder.tvTitle.setText(area.getRegionName());
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        public AddressPager.OnItemClickListener listener;
        public Area area;
        private TextView tvTitle;

        public ViewHolder(final View itemView) {

            super(itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);

            itemView.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
//                            area.setSelected(true);
//                            itemView.setSelected(true);
                            listener.onItemClick(area, getAdapterPosition());
                        }
                    });
        }
    }
}
